<template>
  <el-card class="search">
    <template #header>
      <el-menu
        :default-active="state.activeIndex"
        class="el-menu-demo"
        mode="horizontal"
        @select="handleSelect"
      >
        <el-menu-item index="1">销售额</el-menu-item>
        <el-menu-item index="2">访问量</el-menu-item>
      </el-menu>

      <div class="right">
        <div>
          <el-radio-group v-model="state.time">
            <el-radio-button label="今日" />
            <el-radio-button label="本周" />
            <el-radio-button label="本月" />
            <el-radio-button label="今年" />
          </el-radio-group>
        </div>
        <el-date-picker
          style="margin-left: 20px"
          v-model="state.time2"
          type="daterange"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :shortcuts="shortcuts"
        />
      </div>
    </template>
    <div class="content">
      <v-chart :option="getBarOption()" autoresize></v-chart>
      <div class="rank-list">
        <div class="title">
          {{ state.activeIndex === "1" ? "门店销售额排名" : "门店访问量排名" }}
        </div>
        <div class="list">
          <div class="list-item" v-for="item in rankData" :key="item.no">
            <div class="no" :class="{ 'top-no': item.no <= 3 }">
              {{ item.no }}
            </div>
            <div class="name">{{ item.name }}</div>
            <div class="count">{{ item.count }}</div>
          </div>
        </div>
      </div>
    </div>
  </el-card>
</template>
<script lang="ts">
export default {
  name: "Search",
}
</script>
<script lang="ts" setup>
import { ref, reactive, computed } from "vue"
import { useReportDataStore } from "@/stores/reportData"
import useFormat from "@/hooks/useFormat"
const { numberFormat, moneyFormat } = useFormat()
const reportDataStore = useReportDataStore()
const orderFullYearAxis = computed(
  () => reportDataStore.reportData.orderFullYearAxis
) // 销售额X
const orderFullYear = computed(() => reportDataStore.reportData.orderFullYear) // 销售额Y
const userFullYearAxis = computed(
  () => reportDataStore.reportData.userFullYearAxis
) // 访问量X
const userFullYear = computed(() => reportDataStore.reportData.userFullYear) // 访问量Y
// 排行榜数据
const orderRank = computed(() => reportDataStore.reportData.orderRank) // 门店销售额排行
const userRank = computed(() => reportDataStore.reportData.userRank) // 门店访问量排行

// const activeIndex = ref("1")
// const radio = ref("今日")
// const value2 = ref("")
const state = reactive({
  activeIndex: "1",
  time: "今日",
  time2: "",
})

const handleSelect = (key: string) => {
  // console.log(key)
  state.activeIndex = key
}

const rankData = computed(() =>
  state.activeIndex === "1" ? orderRank.value : userRank.value
)

// 准备生成图标配置内容
const getBarOption = () => {
  const text = state.activeIndex === "1" ? "销售趋势" : "访问量趋势"
  return {
    title: {
      text,
      textStyle: {
        fontSize: 16,
      },
    },
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow",
      },
    },
    grid: {
      left: "3%",
      right: "4%",
      bottom: "3%",
      containLabel: true,
    },
    xAxis: [
      {
        type: "category",
        data:
          state.activeIndex === "1"
            ? orderFullYearAxis.value
            : userFullYearAxis.value,
        axisTick: {
          alignWithLabel: true,
        },
      },
    ],
    yAxis: [
      {
        type: "value",
      },
    ],
    series: [
      {
        name: "Direct",
        type: "bar",
        barWidth: "60%",
        data:
          state.activeIndex === "1" ? orderFullYear.value : userFullYear.value,
      },
    ],
  }
}

const shortcuts = [
  {
    text: "最近一周",
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    },
  },
  {
    text: "最近一月",
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
      return [start, end]
    },
  },
  {
    text: "最近三月",
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
      return [start, end]
    },
  },
]
</script>
<style lang="scss" scoped>
.search {
  margin-top: 20px;
  :deep(.el-card__header) {
    position: relative;
    border-bottom: none;
    padding: 0 20px;
    .right {
      position: absolute;
      top: 16px;
      right: 20px;
      display: flex;
      align-items: center;
    }
  }
  .content {
    display: flex;
    height: 270px;

    .echarts {
      flex: 5;
    }

    .rank-list {
      flex: 2;

      .title {
        font-size: 14px;
        color: #333;
        font-weight: 500;
        margin-top: 20px;
      }

      .list {
        margin-top: 15px;

        .list-item {
          display: flex;
          align-items: center;
          height: 20px;
          font-size: 12px;
          margin: 6px 20px 6px 0;

          .no {
            width: 20px;
            height: 20px;
            color: #333;
            display: flex;
            justify-content: center;
            align-items: center;

            &.top-no {
              border-radius: 50%;
              background-color: #000;
              color: #fff;
              font-weight: 300;
            }
          }

          .name {
            margin-left: 10px;
          }

          .count {
            flex: 1;
            text-align: right;
          }
        }
      }
    }
  }
}
</style>
